package net.betou.cms.action.front;

import static net.betou.cms.Constants.TPLDIR_SPECIAL;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.cms.entity.main.CmsSite;
import net.betou.cms.web.CmsUtils;
import net.betou.cms.web.FrontUtils;
import net.betou.common.web.RequestUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SearchAct{
	public static final String SEARCH_INPUT = "tpl.searchInput";
	public static final String SEARCH_RESULT = "tpl.searchResult";
	public static final String SEARCH_ERROR = "tpl.searchError";
	public static final String SEARCH_JOB = "tpl.searchJob";

	@RequestMapping( value = "/search*.jspx", method = RequestMethod.GET )
	public String index( HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		CmsSite site = CmsUtils.getSite( request );
		// 将request中所有参数保存至model中。
		model.putAll( RequestUtils.getQueryParams( request ) );
		FrontUtils.frontData( request, model, site );
		FrontUtils.frontPageData( request, model );
		String q = RequestUtils.getQueryParam( request, "q" );
		String parseQ = parseKeywords( q );
		model.addAttribute( "input", q );
		model.addAttribute( "q", parseQ );
		String channelId = RequestUtils.getQueryParam( request, "channelId" );
		if( StringUtils.isBlank( q ) && StringUtils.isBlank( channelId ) ){
			return FrontUtils.getTplPath( request, site.getSolutionPath( ), TPLDIR_SPECIAL, SEARCH_INPUT );
		}else{
			return FrontUtils.getTplPath( request, site.getSolutionPath( ), TPLDIR_SPECIAL, SEARCH_RESULT );
		}
	}

	@RequestMapping( value = "/searchJob*.jspx", method = RequestMethod.GET )
	public String searchJob( HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		CmsSite site = CmsUtils.getSite( request );
		String q = RequestUtils.getQueryParam( request, "q" );
		String category = RequestUtils.getQueryParam( request, "category" );
		String workplace = RequestUtils.getQueryParam( request, "workplace" );
		model.putAll( RequestUtils.getQueryParams( request ) );
		FrontUtils.frontData( request, model, site );
		FrontUtils.frontPageData( request, model );
		// 处理lucene查询字符串中的关键字
		String parseQ = parseKeywords( q );
		model.addAttribute( "input", q );
		model.addAttribute( "q", parseQ );
		model.addAttribute( "queryCategory", category );
		model.addAttribute( "queryWorkplace", workplace );
		if( StringUtils.isBlank( q ) ){
			model.remove( "q" );
		}
		return FrontUtils.getTplPath( request, site.getSolutionPath( ), TPLDIR_SPECIAL, SEARCH_JOB );
	}

	public static String parseKeywords( String q ) {
		char c = '\\';
		int cIndex = q.indexOf( c );
		if( cIndex != -1 && cIndex == 0 ){
			q = q.substring( 1 );
		}
		if( cIndex != -1 && cIndex == q.length( ) - 1 ){
			q = q.substring( 0, q.length( ) - 1 );
		}
		try{
			String regular = "[\\+\\-\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\~\\*\\?\\:\\\\]";
			Pattern p = Pattern.compile( regular );
			Matcher m = p.matcher( q );
			String src = null;
			while( m.find( ) ){
				src = m.group( );
				q = q.replaceAll( "\\" + src, ( "\\\\" + src ) );
			}
			q = q.replaceAll( "AND", "and" ).replaceAll( "OR", "or" ).replace( "NOT", "not" );
		}catch( Exception e ){
			q = q;
		}
		return q;
	}
}
